package vg.modules.backlight.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;

public class PathsSearcher {
	private JButton button;
	private AGraphView view;

	public PathsSearcher() {
		this.button = new JButton(new ImageIcon("./data/resources/textures/paths.png"));
		this.button.setToolTipText("Find all paths beetwen two vertices");
		this.button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Graph ssg = view.getGraphInformation().getSelectionSubGraph();
				if (ssg != null) {
					Collection<Vertex> lsv = ssg.getVertices();
					if (lsv != null && lsv.size() == 2) {
						Iterator<Vertex> it = lsv.iterator();
						Vertex sv = it.next();
						Vertex sv2 = it.next();

						Object[] possibilities = { "from " + sv.getStorableId() + " to " + sv2.getStorableId(), "from " + sv2.getStorableId()+ " to " + sv.getStorableId()};
						Object o = JOptionPane.showInputDialog(null, "Path ", "Select direction", JOptionPane.PLAIN_MESSAGE, null, possibilities, possibilities[0]);
						if (o == null)
							return;
						if (o.equals(possibilities[0]))
							view.getGraphAlgorithms().showPaths(sv, sv2);
						else
							view.getGraphAlgorithms().showPaths(sv2, sv);
					}
				}
			}
		});
		this.button.setEnabled(false);
	}

	public JComponent getView() {
		return (this.button);
	}

	public void setEnabled(boolean enable) {
		this.button.setEnabled(enable);
	}

	public void changeView(AGraphView igv) {
		this.view = igv;
		if (view == null) {
			setEnabled(false);
		}
	}
}
